package com.kaili.basic.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseCategoryAddDto;
import com.kaili.basic.pojo.dto.BaseCategoryQueryDto;
import com.kaili.basic.pojo.entity.BaseAllCategory;
import com.kaili.basic.pojo.entity.BaseCategory;
import com.kaili.basic.pojo.entity.BaseCategoryExport;
import com.kaili.basic.service.BaseCategoryService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "类别管理", description = "类别管理相关API")
public class BaseCategoryController {

    @Autowired
    private BaseCategoryService categoryService;

    @PostMapping("/categorys")
    @ApiOperation(value = "新增", notes = "新增类别管理数据")
    public RestStatus addBaseCategory(@Validated @RequestBody BaseCategoryAddDto categoryAddDto) {
        if (categoryAddDto == null) {
            return CommonStatus.BAD_REQUEST;
        }
        categoryService.addCategory(categoryAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/categorys/{categoryId}")
    @ApiOperation(value = "删除", notes = "删除类别管理数据")
    public RestStatus deleteBaseCategory(@PathVariable("categoryId") Long categoryId) {
        if (categoryId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        categoryService.deleteCategory(categoryId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/categorys")
    @ApiOperation(value = "更新", notes = "更新类别管理数据")
    public RestStatus updateBaseCategory(@Validated @RequestBody BaseCategoryAddDto categoryAddDto) {
        if (categoryAddDto != null) {
            categoryService.updateCategory(categoryAddDto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/categorys")
    @ApiOperation(value = "分页查询类别管理数据", notes = "分页查询事件")
    public Page<BaseCategory> queryBaseCategory(@ModelAttribute BaseCategoryQueryDto categoryQueryDto) {
        return categoryService.queryCategory(new Page<BaseCategory>(categoryQueryDto.getPage(), categoryQueryDto.getPageSize()), categoryQueryDto);
    }

    @GetMapping("/categorys/all")
    @ApiOperation(value = "查询所有类别管理数据")
    public List<BaseAllCategory> queryAllCategory() {
        return categoryService.queryAllCategory();
    }

    @GetMapping("/categoryCodes")
    @ApiOperation(value = "编号", notes = "查询编号")
    public String getCategoryCode(){
        String code = categoryService.getCategoryCode();
        if (StringUtils.isEmpty(code)){
            return "00001";
        }
        String result = String.valueOf(Integer.valueOf(code)+1);
        if (result.length() == 1){
            result = "0000" + result;
        }else if (result.length() == 2){
            result = "000" + result;
        }else if (result.length() == 3){
            result = "00" + result;
        }else if (result.length() == 4){
            result = "0" + result;
        }
        return result;
    }

    @GetMapping("/categorys/export")
    @ApiOperation(value = "类别导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseCategoryQueryDto queryDto) throws com.kaili.common.exception.RestException {
        List<BaseCategoryExport> list = categoryService.queryCategoryExport(queryDto);
        ExcelUtil.exportExcel(list,"产品类别信息","sheet1", BaseCategoryExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}
